package murphyhan;

import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.TreeMap;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.PhotoEntry;

public class PicasaPhotoBuilder implements Iterator {

	private TreeMap photoMap = new TreeMap();
	private int index = 0;
	private Iterator photos;
	private String albumUrl;
	private String albumTitle;

	public PicasaPhotoBuilder(String albumid) {
		this.go(albumid);
	}

	public String getAlbumUrl() {
		return albumUrl;
	}

	public String getAlbumTitle() {
		return Placemark.stringToUnicode(albumTitle);
	}

	public void go(String albumid) {

		try {
			PicasawebService myService = new PicasawebService(
					"exampleCo-exampleApp-1");
			// myService.setUserToken("murphyhan");
			// "http://picasaweb.google.com/data/feed/api/user/" + userId +
			// "/albumid/" + albumid
			albumid = albumid.replaceAll("feed/base", "feed/api");
			albumid = albumid.replaceAll("entry/api", "feed/api");

			// albumid =
			// "http://picasaweb.google.com/data/feed/base/user/murphyhan/albumid/5310794913945963457";
			// albumid =
			// "http://picasaweb.google.com/data/feed/api/user/murphyhan/albumid/5310794913945963457";
			URL feedUrl = new URL(albumid);

			AlbumFeed feed = myService.getFeed(feedUrl, AlbumFeed.class);

			albumTitle = feed.getTitle().getPlainText();

			albumUrl = feed.getHtmlLink().getHref();

			for (PhotoEntry photo : feed.getPhotoEntries()) {
				try {
					if (photo.getExifTags().getTime() == null) {
						continue;
					}
				} catch (Exception e) {
					continue;
				}

				Date time = this.getDate(photo.getExifTags().getTime());

				String url = photo.getMediaThumbnails().get(0).getUrl();
				if (photo.getWidth() < 1024) {
					url = "\"" + url.replaceAll("/s72/", "/s400/") + "\"";
				} else {
					url = "\"" + url.replaceAll("/s72/", "/s720/") + "\"";
				}

				Photo pp = new Photo(photo.getTitle().getPlainText(), time,
						url, photo.getDescription().getPlainText());

				System.out.println(pp.toString());
				photoMap.put(time, pp);
			}

			photoMap.put(new Date(), new Photo("", new Date(), "", ""));

			photos = photoMap.values().iterator();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private Date getDate(Date time) throws Exception {

		Calendar now = Calendar.getInstance();
		now.setTime(time);
		// now.add(now.HOUR, -8);

		// return now.getTime();
		return time;

	}

	public boolean hasNext() {

		return photos.hasNext();
	}

	public void remove() {
		photos.remove();

	}

	public java.lang.Object next() {
		index++;
		return photos.next();
	}

	public static void main(String[] args) {

		String rssUrl = "http://picasaweb.google.com/data/feed/base/user/murphyhan/albumid/5243172731343755905?alt=rss&kind=photo&hl=en_US";

		PicasaPhotoBuilder pp = new PicasaPhotoBuilder(rssUrl);
	}

}
